﻿using System.CodeDom;
using System.Linq;

namespace InterfaceWeaver.CustomTool
{
    public static class CodeMemberMethodExtensions
    {
        /// <summary>
        /// Comments the specified method.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="summary">The summary.</param>
        /// <param name="remarks">The remarks.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static CodeMemberMethod Comment(this CodeMemberMethod method, string summary, string remarks = null, params string[] parameters)
        {
            method.Comments.Add(new CodeCommentStatement("<summary>", true));
            method.Comments.Add(new CodeCommentStatement(summary, true));
            method.Comments.Add(new CodeCommentStatement("</summary>", true));

            if(remarks != null)
            {
                method.Comments.Add(new CodeCommentStatement("<remarks>", true));
                method.Comments.Add(new CodeCommentStatement(remarks, true));
                method.Comments.Add(new CodeCommentStatement("</remarks>", true));
            }

            if(parameters != null && parameters.Any())
            {
                for (int i = 0; i < parameters.Count() - 1; i++)
                {
                    method.Comments.Add(new CodeCommentStatement(
                        string.Format("<param name=\"{0}\">{1}</param>", 
                        method.Parameters[i].Name, 
                        parameters[i]), true));
                }
            }

            return method;
        }
    }
}
